package com.example.ccj.b.c.custom.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.alibaba.fastjson.JSON;
import com.example.ccj.R;
import com.example.ccj.b.c.dialog.MultiChoiceListener;
import com.example.ccj.u.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 多项选择加载选项
 * @param <T>
 */
public class MultiChoiceAdapter<T> extends RecyclerView.Adapter<MultiChoiceAdapter.ViewHolder>{
    private final MultiChoiceListener<T> multiChoiceListener;
    private final List<T> userList;
    private final List<T> selectedList;

    public MultiChoiceAdapter(List<T> userList, List<T> selectedList, MultiChoiceListener<T> multiChoiceListener){
        this.selectedList = new ArrayList<>(selectedList);
        this.userList = userList;
        this.multiChoiceListener = multiChoiceListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.adapater_multi_option,
                        parent,
                        false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        T str = userList.get(position);
        if (multiChoiceListener == null) {
            holder.name.setText(str.toString());
            LogUtils.d("未设置多选项的显示文本，建议添加formatter！");
        } else {
            holder.name.setText(multiChoiceListener.formatItem(str));
        }

        holder.is_check.setChecked(selectedList.contains(str));

        holder.is_check.setOnCheckedChangeListener(change(str,position));
    }

    private CompoundButton.OnCheckedChangeListener change(T str,int position){
        return new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //当前是选中状态
                if (isChecked) {
                    multiChoiceListener.onSelect(str,position);
                } else {
                    multiChoiceListener.onRemove(str,position);
                }
            }
        };
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

    static class ViewHolder extends  RecyclerView.ViewHolder{
        private final TextView name;
        private final CheckBox is_check;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            is_check = itemView.findViewById(R.id.is_check);
        }
    }

    public interface ISelectListener<T> {
        void onSelect(T user,int position);
        void onRemove(T user,int position);

    }

}
